using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node translation_unit in the grammar(AnsiC).
    /// </summary>
    internal partial class Vntranslation_unit : IFullFormatter {
        // [203]=translation_unit : external_declaration ;
        // [204]=translation_unit : translation_unit external_declaration ;

        private readonly TokenRange _scope;
        private readonly Vnexternal_declaration first0;
        private readonly List<Vnexternal_declaration> list = new();

        public TokenRange Scope => _scope;

        public void Format(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        public void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            {
                context.PrintBlanksAnd(this.first0, preConfig, writer);
            }
            var first = true;
            var config = new BlankConfig(inlineBlank: 1, forceNewline: false);
            for (var i = 0; i < this.list.Count; i++) {
                var exp = this.list[i];
                if (first) {
                    context.PrintCommentsBetween(this.first0, exp, config, writer);
                    first = false;
                }
                else {
                    context.PrintCommentsBetween(this.list[i - 1], exp, config, writer);
                }
                context.PrintBlanksAnd(exp, config, writer);
            }
        }

        public IEnumerable<string> YieldTokens(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        // [203]=translation_unit : external_declaration ;
        // dumped by ListExtractor 1
        internal Vntranslation_unit(Vnexternal_declaration first0) {
            this.first0 = first0;
            this._scope = new TokenRange(first0);
        }

        // [204]=translation_unit : translation_unit external_declaration ;
        // dumped by ListExtractor 2
        internal void Add(Vnexternal_declaration r0) {
            this.list.Add(r0);
            this._scope.end = r0.Scope.end;
        }

    }
}
